package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.uwm.universitydb.model.Person;
import edu.uwm.universitydb.model.Professor;
import edu.uwm.universitydb.model.Project;
import edu.uwm.universitydb.model.Role;
import edu.uwm.universitydb.model.User;
import edu.uwm.universitydb.service.ServiceFactory;
import edu.uwm.universitydb.service.professor.ProfessorService;
import edu.uwm.universitydb.swing.ProjectTableModel;

public class ProjectMaint extends JPanel {
	
	public ProjectMaint(ProjectTableModel projectTable, User user) {
		_dataTable = projectTable;
		_user = user;
		this.setLayout(new BorderLayout());
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout());
		titlePanel.setBackground(Color.WHITE);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 16, 0, Color.WHITE));
		JLabel title = new JLabel("Project Maintenance");
		title.setFont(new Font(title.getFont().getName(), title.getFont().getStyle(), 18));
		titlePanel.add(title);

		this.add(titlePanel, BorderLayout.NORTH);
		this.add(this.createAppView(), BorderLayout.CENTER);
		
	}
	
	private JPanel createAppView() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		//Create Edit/Delete buttons panel
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		_editButton = new JButton(ProjectMaint.EDITBUTTON);
		_editButton.setIcon(new ImageIcon("res/edit.png"));
		_editButton.setName(ProjectMaint.EDITBUTTON);
		
		_addMemberButton = new JButton(ProjectMaint.ADDMEMBERBUTTON);
		_addMemberButton.setIcon(new ImageIcon("res/add-member.png"));
		_addMemberButton.setName(ProjectMaint.ADDMEMBERBUTTON);
		
		_removeMemberButton = new JButton(ProjectMaint.REMOVEMEMBERBUTTON);
		_removeMemberButton.setIcon(new ImageIcon("res/remove-member.png"));
		_removeMemberButton.setName(ProjectMaint.REMOVEMEMBERBUTTON);
		
		_deleteButton = new JButton(ProjectMaint.DELBUTTON);
		_deleteButton.setIcon(new ImageIcon("res/remove.png"));
		_deleteButton.setName(ProjectMaint.DELBUTTON);
		
		_addButton = new JButton(ProjectMaint.ADDBUTTON);
		_addButton.setIcon(new ImageIcon("res/add.png"));
		_addButton.setName(ProjectMaint.ADDBUTTON);
		
		btnPanel.setBackground(Color.WHITE);
		btnPanel.add(_addButton);
		btnPanel.add(_editButton);
		btnPanel.add(_addMemberButton);
		btnPanel.add(_removeMemberButton);
		btnPanel.add(_deleteButton);
		panel.add(btnPanel);

		//Create Table
		_jtable = new JTable(_dataTable);
		_jtable.setName("DataTbl");
		_jtable.setAlignmentX(Component.CENTER_ALIGNMENT);
		_jtable.setAutoCreateRowSorter(true);
		_jtable.getColumnModel().getColumn(0).setMaxWidth(200);
		_jtable.getColumnModel().getColumn(0).setMinWidth(200);
		_jtable.getColumnModel().getColumn(1).setMaxWidth(200);
		_jtable.getColumnModel().getColumn(1).setMinWidth(200);
		_jtable.getTableHeader().setReorderingAllowed(false);
		
		JScrollPane tblPane = new JScrollPane(_jtable);
		tblPane.getViewport().setBackground(Color.WHITE);
		panel.add(tblPane);
		
		panel.add(Box.createRigidArea(new Dimension(0,16)));
		return panel;
	}
	
	public JPanel showMaintView(Project proj, Professor manage) {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		//Name
		panel.add(new JLabel("Project Number"));
		JTextField num = new JTextField(proj.getProjectNum().toString());
		num.setEditable(false);
		num.setName(NAMEFIELD);
		panel.add(num);
		
		//Sponsor
		panel.add(new JLabel("Sponsor"));
		JTextField sponsor = new JTextField(proj.getSponsor());
		sponsor.setName(SPONSORFIELD);
		panel.add(sponsor);
		
		//Start Date
		panel.add(new JLabel("Start Date  format(YYYY-MM-DD)"));
		JTextField startDate = new JTextField(_dateformat.format(proj.getStartDate()));
		startDate.setName(STARTDATEFIELD);
		panel.add(startDate);
		
		//End Date
		panel.add(new JLabel("End Date  format(YYYY-MM-DD)"));
		JTextField endDate = new JTextField(_dateformat.format(proj.getEndDate()));
		endDate.setName(ENDDATEFIELD);
		panel.add(endDate);
		
		//Budget
		panel.add(new JLabel("Budget"));
		JTextField budget = new JTextField(proj.getBudget().toString());
		budget.setName(BUDGETFIELD);
		panel.add(budget);
		
		//Manager
		panel.add(new JLabel("Principle Investigator"));
		ProfessorService prof = ServiceFactory.getInstance().getProfessorService();
		List<Professor> profList = prof.getAllProfessors();
		JComboBox<Professor> manageProf = new JComboBox<Professor>(profList.toArray(new Professor[profList.size()]));
		manageProf.setName(MANAGE);
		manageProf.setSelectedItem(manage);
		manageProf.setEnabled(false);
		panel.add(manageProf);
		
		return panel;
	}
	
	public JPanel showNewView() {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		//Name
		panel.add(new JLabel("Project Number"));
		JTextField num = new JTextField();
		num.setName(NAMEFIELD);
		panel.add(num);
		
		//Sponsor
		panel.add(new JLabel("Sponsor"));
		JTextField sponsor = new JTextField();
		sponsor.setName(SPONSORFIELD);
		panel.add(sponsor);
		
		//Start Date
		panel.add(new JLabel("Start Date  format(YYYY-MM-DD)"));
		JTextField startDate = new JTextField();
		startDate.setName(STARTDATEFIELD);
		panel.add(startDate);
		
		//End Date
		panel.add(new JLabel("End Date  format(YYYY-MM-DD)"));
		JTextField endDate = new JTextField();
		endDate.setName(ENDDATEFIELD);
		panel.add(endDate);
		
		//Budget
		panel.add(new JLabel("Budget"));
		JTextField budget = new JTextField();
		budget.setName(BUDGETFIELD);
		panel.add(budget);
		
		//Manager
		panel.add(new JLabel("Principle Investigator"));
		ProfessorService prof = ServiceFactory.getInstance().getProfessorService();
		List<Professor> profList = prof.getAllProfessors();
		JComboBox<Professor> manage = new JComboBox<Professor>(profList.toArray(new Professor[profList.size()]));
		manage.setName(MANAGE);
		if (_user.getRole() == Role.ADMINISTRATOR) {
			manage.setSelectedIndex(-1);
		}
		else {
			manage.setSelectedIndex(-1);
			for (Professor p : profList) {
				//firstname
				if (_user.getUserName().equals(p.getName().split(" ")[0])) {
					manage.setSelectedItem(p);
					break;
				}
			}
			manage.setEnabled(false);
		}
		panel.add(manage);
				
		return panel;
	}
	
	public JPanel showMemberView(List<Person> choices) {
		JPanel panel = new JPanel(new GridLayout(0, 1));
		
		//Member
		panel.add(new JLabel("Member"));
		Collections.sort(choices);
		JComboBox<Person> member = new JComboBox<Person>(choices.toArray(new Person[choices.size()]));
		member.setName(MEMBER);
		panel.add(member);
		
		return panel;
	}
	
	public void refreshTable() {
		_dataTable.refreshData();
		_jtable.revalidate();
		_jtable.repaint();		
	}
	
	public JButton getAddButton() {
		return _addButton;
	}
	
	public JButton getEditButton() {
		return _editButton;
	}
	
	public JButton getAddMemberButton() {
		return _addMemberButton;
	}
	
	public JButton getRemoveMemberButton() {
		return _removeMemberButton;
	}
	
	public JButton getDelButton() {
		return _deleteButton;
	}
	
	public JTable getDataTable() {
		return _jtable;
	}
	
	
	private JButton _editButton;
	private JButton _addMemberButton;
	private JButton _removeMemberButton;
	private JButton _deleteButton;
	private JButton _addButton;
	private JTable _jtable;
	
	private ProjectTableModel _dataTable;
	private User _user;
	
	public static final String EDITBUTTON = "Edit";
	public static final String ADDMEMBERBUTTON = "Add Member";
	public static final String REMOVEMEMBERBUTTON = "Remove Member";
	public static final String DELBUTTON = "Delete";
	public static final String ADDBUTTON = "New";
	
	public static final String NAMEFIELD = "NAME";
	public static final String SPONSORFIELD = "SPONSOR";
	public static final String STARTDATEFIELD = "STARTDATE";
	public static final String ENDDATEFIELD = "ENDDATE";
	public static final String BUDGETFIELD = "BUDGET";
	public static final String MANAGE = "MANAGE";
	public static final String MEMBER = "MEMBER";

	private final DateFormat _dateformat = new SimpleDateFormat("yyyy-MM-dd");

	
	private static final long serialVersionUID = -3488920465856523161L;
}
